﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Drawing;

public partial class Main_Reports_ServiceInvoice_TDSDisplay : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    string[] custname = new string[10];
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lblcompname.Text = Session["compname"].ToString();
            BindData();
        }
    }

    protected void BindData()
    {
        if (Request.QueryString != null)
        {
            string tdsreportopt = Request.QueryString["TDSReportOpt"].ToString();
            string tdsviewopt = Request.QueryString["ViewOpt"].ToString();
            string customer = Request.QueryString["Customer"].ToString();
            string custname = Request.QueryString["CustName"].ToString();
            DataTable dt = new DataTable();
            DataRow ds = dt.NewRow();
            dt.Columns.Add("Particulars");
            dt.Columns.Add("Service");
            dt.Columns.Add("TDS Amount");
            dt.Columns.Add("DrillDown");
            dt.Columns.Add("STRate");
            dt.Columns.Add("Cess1Rate");
            dt.Columns.Add("Cess2Rate");
            dt.Columns.Add("ServiceAmt");
            dt.Columns.Add("Cess1Amt");
            dt.Columns.Add("Cess2Amt");
            dt.Columns.Add("CertificateNumber");
            dt.Columns.Add("ReceivedDate", typeof(DateTime));
            dt.Columns.Add("Remarks");
            dt.Columns.Add("Tranid");
            dt.Columns.Add("ChildID");
            dt.Columns.Add("TDSCertificateid");
            dt.Columns.Add("Formatid");
            dt.Columns.Add("Trantype");
            dt.Columns.Add("Transactionmember");
            dt.Columns.Add("Ledgerid");
            dt.Columns.Add("Transactiondate", typeof(DateTime));

            if (tdsreportopt != string.Empty && tdsviewopt != string.Empty && customer != string.Empty)
            {
                int count = 0;
                if (customer.Contains("-") && custname.Contains("-"))
                {
                    string[] customersname = customer.Split('-');
                    string[] custmrname = custname.Split('-');
                    count = customersname.Length;
                    for (int i = 0; i < count; i++)
                    {
                        SqlParameter[] sqlpara = {
                                                 new SqlParameter("@CUSTOMERLEDGERID",customersname[i].ToString())
                                             };
                        DataTable dttemp = DU.getdatausingprocedure("USP_IVC_GetExistingTDS", sqlpara);
                        if (dttemp.Rows.Count > 0)
                        {
                            DataRow dew = dt.NewRow();

                            dt.Rows.Add(custmrname[i].ToString());
                            foreach (DataRow dr in dttemp.Rows)
                            {
                                dt.ImportRow(dr);
                            }

                        }
                    }
                    if (tdsreportopt == "0" && dt.Rows.Count > 0)
                    {
                        DataView dv = dt.DefaultView;
                        dv.RowFilter = "IsNull(RECEIVEDDATE, '') <> ''";
                        DataTable dtchk1 = dv.ToTable();
                        if (dtchk1.Rows.Count > 0)
                        {
                            grdTDSDisplay.DataSource = dtchk1;

                            grdTDSDisplay.DataBind();
                        }
                    }
                    else if (tdsreportopt == "1" && dt.Rows.Count > 0)
                    {
                        DataView dv = dt.DefaultView;
                        dv.RowFilter = "IsNull(RECEIVEDDATE, '') = ''";
                        DataTable dtchk2 = dv.ToTable();
                        if (dtchk2.Rows.Count > 0)
                        {
                            grdTDSDisplay.DataSource = dtchk2;

                            grdTDSDisplay.DataBind();
                        }
                    }
                    else if (tdsreportopt == "2" && dt.Rows.Count > 0)
                    {
                        grdTDSDisplay.DataSource = dt;
                        grdTDSDisplay.DataBind();
                    }
                }

            }

            else
            {
                SqlParameter[] sqlpara = {
                                                 new SqlParameter("@CUSTOMERLEDGERID",customer)
                                             };
                dt = DU.getdatausingprocedure("USP_IVC_GetExistingTDS", sqlpara);
                grdTDSDisplay.DataSource = dt;

                grdTDSDisplay.DataBind();
            }
        }
    }

    protected void grdTDSDisplay_RowDataBound(object sender, GridViewRowEventArgs e)
    {        
        string tdsviewopt = Request.QueryString["ViewOpt"].ToString();
        if (tdsviewopt == "1")
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[2].Visible = false;
                e.Row.Cells[3].Visible = false;
                e.Row.Cells[10].Visible = false;
                e.Row.Cells[11].Visible = false;
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[2].Visible = false;
                e.Row.Cells[3].Visible = false;
                e.Row.Cells[10].Visible = false;
                e.Row.Cells[11].Visible = false;
            }
        }
        else
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[4].Visible = false;
                e.Row.Cells[5].Visible = false;
                e.Row.Cells[6].Visible = false;
                e.Row.Cells[7].Visible = false;
                e.Row.Cells[8].Visible = false;
                e.Row.Cells[9].Visible = false;
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[4].Visible = false;
                e.Row.Cells[5].Visible = false;
                e.Row.Cells[6].Visible = false;
                e.Row.Cells[7].Visible = false;
                e.Row.Cells[8].Visible = false;
                e.Row.Cells[9].Visible = false;
            }
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[1].Text == string.Empty || e.Row.Cells[1].Text == "&nbsp;")
            {
                if (tdsviewopt == "1")
                {
                    e.Row.Cells[0].Attributes.Add("colspan", "8");
                    e.Row.Cells[0].BackColor = System.Drawing.Color.FromArgb(239, 239, 239);
                }
                else
                {
                    e.Row.Cells[0].Attributes.Add("colspan", "6");
                    e.Row.Cells[0].BackColor = System.Drawing.Color.FromArgb(239, 239, 239);
                }
                e.Row.Cells[0].Font.Bold = true;
                e.Row.Cells.RemoveAt(11);
                e.Row.Cells.RemoveAt(10);
                e.Row.Cells.RemoveAt(9);
                e.Row.Cells.RemoveAt(8);
                e.Row.Cells.RemoveAt(7);
                e.Row.Cells.RemoveAt(6);
                e.Row.Cells.RemoveAt(5);
                e.Row.Cells.RemoveAt(4);
                e.Row.Cells.RemoveAt(3);
                e.Row.Cells.RemoveAt(2);
                e.Row.Cells.RemoveAt(1);
            }
        }
    }


}


